﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using General.Core.Data;
using General.Entities.Data;
using General.Entities.Models;
using Microsoft.EntityFrameworkCore;

namespace General.Services.{TableName}
{
    public class {ServiceName} : I{ServiceName}
    {
        private readonly IRepository<{TableName}> {TableName}Repository;

        public {ServiceName}(IRepository<{TableName}> {TableName}Repository)
        {
            this.{TableName}Repository = {TableName}Repository;
        }


        public async Task<AdminResult> GetList(int page, int limit)
        {
            AdminResult result = new AdminResult();
            var query = from d in {TableName}Repository.Table
                        orderby d.id descending
                        select new {TableName}() {
                             {Parms}
                        };
            result.count = await query.CountAsync();
            result.data = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
            result.code = 0;
            return result;
        }



        public async Task AddInfo({TableName} model)
        {
            model.createtime = DateTime.Now;
            await {TableName}Repository.insertAsync(model);
        }


        public async Task<int> DeleteInfo(string ids)
        {
            var arr = ids.Split(",", StringSplitOptions.RemoveEmptyEntries).Select<string, int>(x => Convert.ToInt32(x));
            var list = await {TableName}Repository.GetAllAsync(x => arr.Contains(x.id)).ToListAsync();
            return await {TableName}Repository.DeleteListAsync(list);
        }


        public async Task EditInfo(int id, {TableName} data)
        {
            var model = await {TableName}Repository.getByIdAsync(id);
{Parms2}
            await {TableName}Repository.UpdateAsync(model);
        }


    }
}
